package helper;

import javax.swing.ImageIcon;
import model.board.*;
import model.players.*;

/** Kelas helper untuk permainan, fungsi-fungsinya:
 *  1. menampilkan berapa jumlah airport yang dimiliki,
 *  2. mengecek apakah punya 1 kompleks,
 *  3. mengeluarkan integer lokasi pariwisata di aset,
 *  4. mengecek apakah lokasi pariwisata terdapat di Kotak[] dan mengeluarkan indeksnya jika ada,
 *  5. mendefrag aset
 *  6. membuat image icon dari string
 *  7. mengecek apakah ada sisa villa atau hotel di suatu kompleks
 */

public class SearchEngine {

//MENGECEK BERAPA JUMLAH AIRPORT
    public static int howManyAirportsDoYouHave(Player P)
    {
        int retval = 0 ;

        for(int i = 1;i<P.jum_aset+1;i++)
        {
            if((P.aset[i] == 5 ) || (P.aset[i] == 13) || (P.aset[i] == 21) ||(P.aset[i] ==  29))
                retval += 1 ;
           
        }
        return retval ;
    }

  public static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = model.Game.class.getResource(path);
        if (imgURL != null)
        {
            return new ImageIcon(imgURL);
        } 
        else
        {
            return null;
        }
    }

//MENGECEK APAKAH PUNYA SATU KOMPLEKS
    public static boolean DoYouHaveThisComplex(Kotak[] Board,int posisi,Player P)
    {
        boolean retval = false ;

        switch (posisi)
        {
            //kompleks A
            case 2: if(Board[3].getOwner()==P)
                    retval = true ;break;
            case 3: if(Board[2].getOwner()==P)
                    retval = true ;break;

            //kompleks B
            case 6: if((Board[7].getOwner()==P)&&(Board[8].getOwner()==P))
                        retval = true ;break;
            case 7: if((Board[6].getOwner()==P)&&(Board[8].getOwner()==P))
                        retval = true ;break;
            case 8: if((Board[7].getOwner()==P)&&(Board[6].getOwner()==P))
                        retval = true ;break;

            //kompleks C
            case 10:if(Board[11].getOwner()==P)
                        retval = true ;break;
            case 11:if(Board[10].getOwner()==P)
                        retval = true ;break;

            //kompleks D
            case 14: if((Board[15].getOwner()==P)&&(Board[16].getOwner()==P))
                        retval = true ;break;
            case 15: if((Board[14].getOwner()==P)&&(Board[16].getOwner()==P))
                        retval = true ;break;
            case 16: if((Board[14].getOwner()==P)&&(Board[15].getOwner()==P))
                        retval = true ;break;

            //kompleks E
            case 19: if((Board[20].getOwner()==P)&&(Board[22].getOwner()==P))
                        retval = true ;break;
            case 20: if((Board[19].getOwner()==P)&&(Board[22].getOwner()==P))
                        retval = true ;break;
            case 22: if((Board[19].getOwner()==P)&&(Board[20].getOwner()==P))
                        retval = true ;break;

            //kompleks F
            case 24: if((Board[26].getOwner()==P)&&(Board[28].getOwner()==P))
                        retval = true ;break;
            case 26: if((Board[24].getOwner()==P)&&(Board[28].getOwner()==P))
                        retval = true ;break;
            case 28: if((Board[24].getOwner()==P)&&(Board[26].getOwner()==P))
                        retval = true ;break;

            //kompleks G
            case 30:if(Board[32].getOwner()==P)
                        retval = true ;break;
            case 32:if(Board[30].getOwner()==P)
                        retval = true ;break;
        }
        return retval ;
    }

//MENGELUARKAN INDEKS LOKASI PARIWISATA DI ASET
    public static int WhereThisLPisinYourAset (int posisiLP, Player current_plyr)
    {
        int idx_aset = 0;
        int i = 1;
        while ( (i <= current_plyr.jum_aset+1) && ((current_plyr.aset[i] != posisiLP)) )
        {
            i++;
        }
        if (current_plyr.aset[i] == posisiLP)
            idx_aset = i;
        return idx_aset;
        
    }

//MENGELUARKAN NILAI INDEKS LOKASI
    public static int LokasionBoard (Kotak[] Board, String s)
    {
        int lokasi = 0;
        for (int i = 1;i<=32;i++)
           if(Board[i].getNama().toLowerCase().equals(s.toLowerCase()))
               lokasi = i;

        return lokasi;
    }

//MENDEFRAG ASET
    public static void Asset_Defragmenter (Player P)
    {
        for (int i = 1; i <= P.jum_aset; i++)
        {
            if (P.aset[i] == 0)
            {
                int j = i;
                while (j < P.jum_aset)
                {
                    P.aset[j] = P.aset[j+1];
                    j++;
                }
                P.jum_aset -= 1;
            }
        }
    }

    public static void QueuePlayerDefragmenter(Players Q)
    {
        for (int i = 1; i <= Q.jum_Player; i++)
        {
            if (Q.player[i] == null)
            {
                int j = i;
                while (j < Q.jum_Player)
                {
                    Q.player[j] = Q.player[j+1];
                    j++;
                }
                Q.jum_Player -= 1;
            }
        }
    }

//MENGECEK APAKAH MASIH ADA VILLA ATAU HOTEL DI SUATU KOMPLEKS
    public static boolean DoYouHaveZeroVillasOrHotelsRemainedInYourComplex(Player P,Kotak[] Board,int LP_yang_mau_dijual)
    {
        boolean retval = true;
        boolean [] temp = new boolean[3] ; 
        int x = 0;
        int counter = 0;

        //pertama2 kita anggep kalo ga punya rumah sama skali...
        for(int j =0;j<3;j++)
            temp[j] = true ;

        //trus kalo tiba2 ada rumah,kita langsung set temp jadi false,sehingga retval nya false
        for(int i =1 ; i<=32 ;i++)
        {
            if(Board[i].getTipe() == 2) //ngecek dulu kotak pariwisata bukan
            {
                LokasiPariwisata checkedKotak = (LokasiPariwisata) Board[i];
                if(checkedKotak.getKompleks().equals(Board[LP_yang_mau_dijual].getKompleks()))
                {
                    if ((checkedKotak.getJum_hotel() != 0)||(checkedKotak.getJum_villa() != 0))
                        temp[x] = false ;
                        x++;
                        counter++;
                }
            }
            //KONDISI : SATU KOMPLEKS TIGA LOKASI PARIWISATA
            if(counter==3)
                retval = temp[0] && temp[1] && temp[2];
            //LOKASI : SATU KOMPLEKS DUA LOKASI PARIWISATA
            else if (counter == 2)
                retval = temp[0] && temp[1];
        }
        return retval ;
    }

    //==========================================================================
    //MAIN DRIVER
    
    public static void main (String[] args)
    {
        if (XMLReader.parseToArrLP("lokasi_pariwisata.xml", 18))
        {
        Kotak[] Board = new Kotak[33];
        for (int i = 1; i<=32; i++)
            {
                Board[i] = new Kotak();
                Board[i] = Board[i].newKotak(i);
            }
       
        Player P1 = new Player(1, "A", "images1.gif", true);

        //BELI SATU AIRPORT
        System.out.println(Board[5].BeliAset(P1));
        System.out.println("Jumlah airport yang dimiliki "+SearchEngine.howManyAirportsDoYouHave(P1));

        //BELI SATU KOMPLEKS
        System.out.println(Board[2].BeliAset(P1));
        System.out.println(Board[3].BeliAset(P1));
        if(SearchEngine.DoYouHaveThisComplex(Board, 2, P1))
            System.out.println("Selamat anda punya 1 kompleks");

        //TEST POSISI KOTAK 3 DI ASET Player
        System.out.println("Posisi di aset: "+SearchEngine.WhereThisLPisinYourAset(Board[3].getPosisi()-1, P1));

        //TEST LOKASI ON Kotak[]
        System.out.println("Indeks lokasi di Kotak[]: "+SearchEngine.LokasionBoard(Board, "Parangtritis"));

        //TEST JUMLAH VILLA OR HOTEL
        if(SearchEngine.DoYouHaveZeroVillasOrHotelsRemainedInYourComplex(P1, Board, 3))
            System.out.println("Tidak ada villa atau hotel di satu kompleks");
        else
            System.out.println("Masih ada villa atau hotel di satu kompleks");

        SearchEngine.Asset_Defragmenter(P1);
       
        }
    }
}//end class

